//
//  LBNewAnselFilter.m
//  OpenGLGame
//
//  Created by Nguyen Cong Huy on 1/22/13.
//  Copyright (c) 2013 UIT. All rights reserved.
//

#include "LBNewAnselFilter.h"

char newAnselFilterFragStr[] = ""

"precision mediump float;"

"varying lowp vec2 vtexCoor;"
"uniform sampler2D texture;"



"vec4 exposure(vec4 color, float expo){"
"   vec4 result = vec4(color.rgb * pow(2.0, expo), color.a);"
"   return result;"
"}"

// vignette function
"vec4 vignette(vec4 color, vec4 vignetteColor, vec2 center, highp float begin, highp float end, float intensity, vec2 texCoor){"
"   float d = distance(texCoor, vec2(center.x, center.y));"
"   float percent = smoothstep(begin, end, d) * intensity;"
"   vec4 result = vec4(mix(color.r, vignetteColor.x, percent), mix(color.y, vignetteColor.y, percent), mix(color.z, vignetteColor.z, percent), 1.0);"
"   return result;"
"}"
// END vignette function

//saturation function (default 1.0)
"vec4 saturationColor(vec4 color, float saturation){"
"  float avg = (color.r + color.g + color.b)/3.0; "
"  color.r = avg + saturation*(color.r - avg); "
"  color.g = avg + saturation*(color.g - avg);"
"  color.b = avg + saturation*(color.b - avg);"
"   color = clamp(color, 0.0, 1.0);"
"  return color;"
"}"
//END saturation function

"void main()"
"{"
"   lowp vec4 textureColor = texture2D(texture, vtexCoor);"
"   vec4 result = textureColor;"

"   result = saturationColor(textureColor, 0.0);"
"   result = exposure(result, 0.2);"
"   result = vignette(result, vec4(0.1, 0.1, 0.1, 1.0), vec2(0.5, 0.5), 0.47, 0.78, 1.0, vtexCoor);"
"    gl_FragColor = result;"
"}";

LBNewAnselFilter::LBNewAnselFilter(){
    init(0, newAnselFilterFragStr);
}